﻿using System;
using System.Dynamic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace wpf.Triage.Behavior
{
    public class BodyImageBehavior : Behavior<Image>
    {
        #region 依赖属性
        /// <summary>
        /// 编辑触发事件
        /// </summary>
        public Action<dynamic> EditCommand
        {
            get { return (Action<dynamic>)GetValue(EditCommandProperty); }
            set { SetValue(EditCommandProperty, value); }
        }
        
        public static readonly DependencyProperty EditCommandProperty =
            DependencyProperty.Register("EditCommand", typeof(Action<dynamic>), typeof(BodyImageBehavior), new PropertyMetadata(null));

        #endregion

        /// <summary>
        ///附加事件
        /// </summary>
        protected override void OnAttached()
        {
            AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
            AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;
        }
        /// <summary>
        /// 移除事件
        /// </summary>
        protected override void OnDetaching()
        {
            AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObject_PreviewMouseLeftButtonDown;
            AssociatedObject.PreviewMouseMove -= AssociatedObject_PreviewMouseMove;
        }
        /// <summary>
        /// 鼠标在图片上移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            System.Windows.Point pp = e.GetPosition(e.Source as FrameworkElement);//WPF方法
            if (EditCommand != null)
            {
                dynamic obj = new ExpandoObject();
                obj.Command = "Move";
                obj.Point = pp;
                EditCommand(obj);
            }
        }

        /// <summary>
        /// 鼠标在图片上点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (EditCommand != null)
            {
                dynamic obj = new ExpandoObject();
                obj.Command = "Click";
                EditCommand(obj);
            }
        }     
    }
}
